package com.mao.crowd.mvc.controller;

import com.mao.crowd.service.api.AdminService;
import com.mao.crowd.service.api.AuthService;
import com.mao.crowd.service.api.RoleService;
import com.mao.crowd.util.ResultEntity;
import entity.Auth;
import entity.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 用来处理admin与role的对应关系
 * @author maomeng
 * @create 2021-06-04- 14:55
 */
@Controller
public class AssignController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private AuthService authService;


    /**
     * 编辑用户角色页面，提交保存用户角色关系的方法
     * @param adminId
     * @param pageNum
     * @param keyword
     * @param roleIdList
     * @return
     */
    @RequestMapping("/assign/do/role/assign.html")
    public String saveAdminRoleRelationship(
            @RequestParam("adminId") Integer adminId,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("keyword") String keyword,
            // 用户可以没有任何角色，不提供请求参数也没问题
            @RequestParam(value = "roleIdList",required = false) List<Integer> roleIdList){

            adminService.saveAdminRoleRelationship(adminId,roleIdList);

        // 保存用户角色关系的方法
        return "redirect:/admin/get/page.html?pageNum=" + pageNum + "&keyword=" + keyword;
    }

    /**
     * 编辑用户角色页面，用来显示所有已注册用户和未注册用户的方法
     * @param adminId
     * @param modelMap
     * @return
     */
    @RequestMapping("/assign/to/assign/role/page.html")
    public String toAssignRolePage(
            @RequestParam("adminId") Integer adminId,
            ModelMap modelMap
            ){
        // 查询已分配角色
        List<Role> assignedRoleList = roleService.getAssignedRole(adminId);

        // 查询未分配角色
        List<Role> unassignedRoleList = roleService.getUnassignedRole(adminId);

        // 两个列表都存入模型，供前端调用
        modelMap.addAttribute("assignedRoleList",assignedRoleList);
        modelMap.addAttribute("unassignedRoleList",unassignedRoleList);
        return "assign-role";
    }

    /**
     * 查询前端全部的auth树形结构
     * @return
     */
    @ResponseBody
    @RequestMapping("/assign/get/all/auth.json")
    public ResultEntity<List<Auth>> getAllAuth(){

        List<Auth> authList = authService.getAllAuth();
        return ResultEntity.successWithData(authList);
    }

    /**
     * 查询需要回显的authId集合
     * @param roleId
     * @return
     */
    @ResponseBody
    @RequestMapping("/assign/get/assigned/auth/by/role/id.json")
    public ResultEntity<List<Integer>> getAssignedAuthIdByRoleId(
            @RequestParam("roleId") Integer roleId
    ){
        List<Integer> authIdList = authService.getAssignedAuthIdByRoleId(roleId);
        return ResultEntity.successWithData(authIdList);
    }

    @ResponseBody
    @RequestMapping("/assign/do/role/assign/auth.json")
    public ResultEntity<String> saveRoleAuthRelationship(@RequestBody Map<String,List<Integer>> map){

        authService.saveRoleAuthRelationship(map);

        return ResultEntity.successWithOutData();
    }
}
